// Author: Mike Moore

var LOCATION_SEARCH_DIV_ID = 'location_search_div';
var LOCATION_SEARCH_CAPTION_ID = 'location_search_caption';
var locationPickParamName = null;



function LocationSearchDiv() {
   this.showLocationSearchDiv = showLocationSearchDiv;
   this.closeLocationDiv = closeLocationDiv;
   this.findLocationMatches = findLocationMatches;
}


function getLocationSearchDiv() {
    
    log('getLocationSearchDiv() called - id = ' + LOCATION_SEARCH_DIV_ID);
    var locationSearchDiv = document.getElementById(LOCATION_SEARCH_DIV_ID);
    if (locationSearchDiv == null) {
    	log('*** ERROR - searchDiv is null - elementId = ' + LOCATION_SEARCH_DIV_ID);
    }
    return(locationSearchDiv);
}

function getLocationCaption() {
    
    var caption = document.getElementById(LOCATION_SEARCH_CAPTION_ID);
    return(caption);
}

//
function showLocationSearchDiv(left, top, caption, paramName) {
	log("Displaying search div");
	locationPickParamName = paramName;
	var searchDiv = getLocationSearchDiv();
	searchDiv.style.visibility = 'visible';

	searchDiv.style.height = 400;
	searchDiv.style.position = 'absolute';
	searchDiv.style.width = 400;
	searchDiv.style.left = left;
	searchDiv.style.top = top;
	searchDiv.style.border = 'solid 1px #222222';
	searchDiv.style.zindex = 5;

	getLocationCaption().innerHTML = caption;
}



function closeLocationDiv() {
	log('closeLocationDiv()');
	var searchDiv = getLocationSearchDiv();
	searchDiv.style.visibility = 'hidden';
	log('finished closeLocationDiv() - display = ' + searchDiv.style.display );

}



 function clearLocationSearch() {
	var lastNameEdit = document.getElementById("edit_lastName");
	lastNameEdit.value = '';

	var firstNameEdit = document.getElementById("edit_firstName");
	firstNameEdit.value = '';

	var phoneEdit = document.getElementById("edit_phoneNumber");
	phoneEdit.value = '';

	var cityEdit = document.getElementById("edit_city");
	cityEdit.value = '';

	var stateEdit = document.getElementById("edit_state");
	stateEdit.value = '';

	document.getElementById("location_search_results").innerHTML = '';
 }

 function findLocationMatches(contextRoot, user, domainId, pcode) {
	log('findLocationMatches() called');
	var locationEdit = document.getElementById("edit_locationName");
	var location = locationEdit.value;

	var href = contextRoot + '/ws?command=' + 'get.matching.locations' + '&user='
	     + user +  '&domainId=' + domainId + '&pcode=' + pcode + '&name='
	     + location;
	log('findLocationMatches()  - href: ' + href);
	remoteCall(href, handleLocationXmlSearchResponse, locationRemoteCallError);
 } 
 
 function locationRemoteCallError(href) {
 	alert('An error occurred attempting to contact the remote server.  Data call: ' + href);
 }

function findMatchesRemoteCall(lastName) {

}

function selectLocationItem(id, locationName) {
	log('Selected Item: ' + id);
	log('Select Code: locationId');
	receiveLocationPick(id, locationName);
	closeLocationDiv();	
}



function handleLocationXmlSearchResponse() {
  // if the readyState code is 4 (Completed)
  // and http status is 200 (OK) we go ahead and get the responseText
  // other readyState codes:
  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
  log("Location response received from server, processing XML response.");
  if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
				xmlDoc =
					AjxXmlDoc.createFromXml(xmlhttp.responseText);

				nativeDocument = xmlDoc.getDoc();
				currentPersonNodes = nativeDocument.getElementsByTagName("location");
				displayReturnedLocationNodes(currentPersonNodes);
		} else {
			log("Not processed - Response status = " + xmlhttp.status);
		}
  } else {
		log("Not processed - Response readyState = " + xmlhttp.readyState);
  }
}


function displayReturnedLocationNodes(currentPersonNodes) {
	var normalRowColor = '#ffffff';
	var selectRowColor = '#cccccc';
	var quotedNormalRowColor = "'" + normalRowColor + "'";
	var quotedSelectRowColor = "'" + selectRowColor + "'";

	var resultsTable = '<table width="100%" style="cursor: pointer">';
	resultsTable += '<tr>'
		+ '<td><font class="subFont"><b>Id</b></font></td>'
		+ '<td><font class="subFont"><b>Name</b></font></td>'
		+ '<td><font class="subFont"><b>Type</b></font></td>'
		+ '</tr>';
	for (i = 0; i < currentPersonNodes.length; i++) {
		node = currentPersonNodes[i];
		var quotedId = "'" + node.getAttribute("id") + "'";
		var quotedName = "'" + node.getAttribute("name") + "'";
		log('quotedId=' + quotedId);
		var trStr;
		trStr = '<tr'
		trStr += ' bgcolor='  + quotedNormalRowColor;
		trStr += ' onClick="selectLocationItem(' + quotedId + ', ' + quotedName + ');"';
		trStr += ' onMouseOver="changeColor(this, '	+ quotedSelectRowColor + ');"';
		trStr += ' onMouseOut="changeColor(this, '	+ quotedNormalRowColor + ');"';
		trStr += '>';
		log(trStr);

		var detailStr = trStr
			+ '<td><font class="subFont">' + node.getAttribute("id") + '</font></td>'
			+ '<td><font class="subFont">' + node.getAttribute("name") + '</font></td>'
			+ '<td><font class="subFont">' + node.getAttribute("typeCode") + '</font></td>'
			+ '</tr>';
		resultsTable += detailStr;
	}
	resultsTable += '</table>';
	log('Setting location table to new contents');
        document.getElementById("location_search_results").innerHTML = resultsTable;
	log('Done - Setting location table to new contents');
}





//Method to receive location pick dialog selection
function receiveLocationPick(locationId, locationName) {
    var paramName = locationPickParamName;
   log('Received location pick  paramName: ' + paramName + '  LocationId=' + locationId);
   var hidden = document.getElementById(paramName);
   
   hidden.value = locationId;
   var inputId = 'location_picker_' + paramName;
   log('Finding DIV that displays location name - element id = ' + inputId);
   var locationDiv = document.getElementById(inputId);
   locationDiv.innerHTML = '<font class="stdFont">' + locationName + '</font>';
};


